/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_stk.c
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides isr stk protection program
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2024-01-24  |V2.0     |Wang Junxiong|New creat
 *****************************************************************************/

/******************************************************************************
 **                     QAC Warnings
 *****************************************************************************/
/* PRQA S 0436 EOF #8-bit MCU Features: Direct Write Registers. */
/* PRQA S 1291, 1820, 1821, 1822, 1823, 1851, 1853, 3715, 3757, 3760 EOF #Registers are essentially unsigned numbers. */
/* PRQA S 2754 EOF #Level 7 problem due to unsupported assembly instructions. */
/* PRQA S 3768, 3771, 4130, 4393, 4434, 4443, 4446, 4461, 4532 EOF #Registers are essentially unsigned numbers. */

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx_stk.h"
/******************************************************************************
 *                      Macro Definitions
 *****************************************************************************/

/******************************************************************************
 *                      Constants
 *****************************************************************************/

/******************************************************************************
 *                      Type definitions
 *****************************************************************************/

/******************************************************************************
 *                      Variables
 *****************************************************************************/

/**
 * @brief If the maximum use is prompted as STK6, the number of 0~6 definitions should be 7.
 *
 */
/* PRQA S 3211 2 # Used in assembly. */
volatile uint8_t H_STKBUFFED[H_STKSAVECOUNT];
volatile uint8_t L_STKBUFFED[L_STKSAVECOUNT];
/******************************************************************************
 *                      Function definitions
 *****************************************************************************/

/**
 * @brief If 1 and interrupt call, the maximum STK in the analyzed interrupt contains the use here, first use 0 to
 * analyze the parameter transfer of the program
 *
 */
#if STK_PROTECTION_STATE

/**
 * @brief Interrupts use stack protection before calling with parameters
 *
 */
void H_ISR_PUSH_STK(void)
{
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277 ++ #Assembly code does not conform to C rules. */
    /* PRQA S 3110, 3112, 2000, 3202, 3109, 3139, 3417, 2015, 2214, 2753, 3426, 1842 ++ #assembly syntax. */
    /* PRQA S 2741 ++ #Control via macro definitions. */
    __asm
      /* Sample for high level interrupt 0x04 entry, for low level interrupt 0x14 entry must use _L_STKBUFFED */
      .define usebuffed_Pu _H_STKBUFFED;
    .if H_STKSAVECOUNT > 0;
    BANKSEL STK00;
    MOV     R0, STK00;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 0, R0;
    .endif;
    .if H_STKSAVECOUNT > 1;
    BANKSEL STK01;
    MOV     R0, STK01;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 1, R0;
    .endif;
    .if H_STKSAVECOUNT > 2;
    BANKSEL STK02;
    MOV     R0, STK02;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 2, R0;
    .endif;
    .if H_STKSAVECOUNT > 3;
    BANKSEL STK03;
    MOV     R0, STK03;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 3, R0;
    .endif;
    .if H_STKSAVECOUNT > 4;
    BANKSEL STK04;
    MOV     R0, STK04;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 4, R0;
    .endif;
    .if H_STKSAVECOUNT > 5;
    BANKSEL STK05;
    MOV     R0, STK05;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 5, R0;
    .endif;
    .if H_STKSAVECOUNT > 6;
    BANKSEL STK06;
    MOV     R0, STK06;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 6, R0;
    .endif;
    .if H_STKSAVECOUNT > 7;
    BANKSEL STK07;
    MOV     R0, STK07;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 7, R0;
    .endif;
    .if H_STKSAVECOUNT > 8;
    BANKSEL STK08;
    MOV     R0, STK08;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 8, R0;
    .endif;
    .if H_STKSAVECOUNT > 9;
    BANKSEL STK09;
    MOV     R0, STK09;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 9, R0;
    .endif;
    .if H_STKSAVECOUNT > 10;
    BANKSEL STK10;
    MOV     R0, STK10;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 0xA, R0;
    .endif;
    .if H_STKSAVECOUNT > 11;
    BANKSEL STK11;
    MOV     R0, STK11;
    BANKSEL usebuffed_Pu;
    MOV     usebuffed_Pu + 0xB, R0;
    .endif;

    __endasm;
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277, 3110, 3112, 2000, 2741, 3202, 3109, 3417, 3139-- */
    /* PRQA S 2015, 2214, 2753, 3426, 1842-- */
}

/**
 * @brief End of Interrupt Processing Calls Stack Processing
 *
 */
void H_ISR_POP_STK(void)
{
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277 ++ #Assembly code does not conform to C rules. */
    /* PRQA S 3110, 3112, 2000, 3202, 3109, 3139, 3417, 2015, 2214, 2753, 3426, 1842 ++ #assembly syntax. */
    /* PRQA S 2741 ++ #Control via macro definitions. */
    __asm
      /* Sample for high level interrupt 0x04 entry, for low level interrupt 0x14 entry must use _L_STKBUFFED */
      .define usebuffed_Po _H_STKBUFFED;
    .if H_STKSAVECOUNT > 0;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 0;
    BANKSEL STK00;
    MOV     STK00, R0;
    .endif;
    .if H_STKSAVECOUNT > 1;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 1;
    BANKSEL STK01;
    MOV     STK01, R0;
    .endif;
    .if H_STKSAVECOUNT > 2;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 2;
    BANKSEL STK02;
    MOV     STK02, R0;
    .endif;
    .if H_STKSAVECOUNT > 3;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 3;
    BANKSEL STK03;
    MOV     STK03, R0;
    .endif;
    .if H_STKSAVECOUNT > 4;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 4;
    BANKSEL STK04;
    MOV     STK04, R0;
    .endif;
    .if H_STKSAVECOUNT > 5;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 5;
    BANKSEL STK05;
    MOV     STK05, R0;
    .endif;
    .if H_STKSAVECOUNT > 6;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 6;
    BANKSEL STK06;
    MOV     STK06, R0;
    .endif;
    .if H_STKSAVECOUNT > 7;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 7;
    BANKSEL STK07;
    MOV     STK07, R0;
    .endif;
    .if H_STKSAVECOUNT > 8;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 8;
    BANKSEL STK08;
    MOV     STK08, R0;
    .endif;
    .if H_STKSAVECOUNT > 9;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 9;
    BANKSEL STK09;
    MOV     STK09, R0;
    .endif;
    .if H_STKSAVECOUNT > 10;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 0x0A;
    BANKSEL STK10;
    MOV     STK10, R0;
    .endif;
    .if H_STKSAVECOUNT > 11;
    BANKSEL usebuffed_Po;
    MOV     R0, usebuffed_Po + 0x0B;
    BANKSEL STK11;
    MOV     STK11, R0;
    .endif;
    __endasm;
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277, 3110, 3112, 2000, 2741, 3202, 3109, 3417, 3139-- */
    /* PRQA S 2015, 2214, 2753, 3426, 1842-- */
}

/**
 * @brief Interrupts use stack protection before calling with parameters
 *
 */
void L_ISR_PUSH_STK(void)
{
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277 ++ #Assembly code does not conform to C rules. */
    /* PRQA S 3110, 3112, 2000, 3202, 3109, 3139, 3417, 2015, 2214, 2753, 3426, 1842 ++ #assembly syntax. */
    /* PRQA S 2741 ++ #Control via macro definitions. */
    __asm
      /* Sample for high level interrupt 0x04 entry, for low level interrupt 0x14 entry must use _L_STKBUFFED */
      .define usebuffed_LPu _L_STKBUFFED;
    .if L_STKSAVECOUNT > 0;
    BANKSEL STK00;
    MOV     R0, STK00;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 0, R0;
    .endif;
    .if L_STKSAVECOUNT > 1;
    BANKSEL STK01;
    MOV     R0, STK01;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 1, R0;
    .endif;
    .if L_STKSAVECOUNT > 2;
    BANKSEL STK02;
    MOV     R0, STK02;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 2, R0;
    .endif;
    .if L_STKSAVECOUNT > 3;
    BANKSEL STK03;
    MOV     R0, STK03;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 3, R0;
    .endif;
    .if L_STKSAVECOUNT > 4;
    BANKSEL STK04;
    MOV     R0, STK04;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 4, R0;
    .endif;
    .if L_STKSAVECOUNT > 5;
    BANKSEL STK05;
    MOV     R0, STK05;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 5, R0;
    .endif;
    .if L_STKSAVECOUNT > 6;
    BANKSEL STK06;
    MOV     R0, STK06;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 6, R0;
    .endif;
    .if L_STKSAVECOUNT > 7;
    BANKSEL STK07;
    MOV     R0, STK07;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 7, R0;
    .endif;
    .if L_STKSAVECOUNT > 8;
    BANKSEL STK08;
    MOV     R0, STK08;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 8, R0;
    .endif;
    .if L_STKSAVECOUNT > 9;
    BANKSEL STK09;
    MOV     R0, STK09;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 9, R0;
    .endif;
    .if L_STKSAVECOUNT > 10;
    BANKSEL STK10;
    MOV     R0, STK10;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 0x0A, R0;
    .endif;
    .if L_STKSAVECOUNT > 11;
    BANKSEL STK11;
    MOV     R0, STK11;
    BANKSEL usebuffed_LPu;
    MOV     usebuffed_LPu + 0x0B, R0;
    .endif;

    __endasm;
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277, 3110, 3112, 2000, 2741, 3202, 3109, 3417, 3139-- */
    /* PRQA S 2015, 2214, 2753, 3426, 1842-- */
}

/**
 * @brief End of Interrupt Processing Calls Stack Processing
 *
 */
void L_ISR_POP_STK(void)
{
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277 ++ #Assembly code does not conform to C rules. */
    /* PRQA S 3110, 3112, 2000, 3202, 3109, 3139, 3417, 2015, 2214, 2753, 3426, 1842 ++ #assembly syntax. */
    /* PRQA S 2741 ++ #Control via macro definitions. */
    __asm;
    .define usebuffed_LPo _L_STKBUFFED;

    .if L_STKSAVECOUNT > 0;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 0;
    BANKSEL STK00;
    MOV     STK00, R0;
    .endif;
    .if L_STKSAVECOUNT > 1;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 1;
    BANKSEL STK01;
    MOV     STK01, R0;
    .endif;
    .if L_STKSAVECOUNT > 2;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 2;
    BANKSEL STK02;
    MOV     STK02, R0;
    .endif;
    .if L_STKSAVECOUNT > 3;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 3;
    BANKSEL STK03;
    MOV     STK03, R0;
    .endif;
    .if L_STKSAVECOUNT > 4;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 4;
    BANKSEL STK04;
    MOV     STK04, R0;
    .endif;
    .if L_STKSAVECOUNT > 5;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 5;
    BANKSEL STK05;
    MOV     STK05, R0;
    .endif;
    .if L_STKSAVECOUNT > 6;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 6;
    BANKSEL STK06;
    MOV     STK06, R0;
    .endif;
    .if L_STKSAVECOUNT > 7;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 7;
    BANKSEL STK07;
    MOV     STK07, R0;
    .endif;
    .if L_STKSAVECOUNT > 8;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 8;
    BANKSEL STK08;
    MOV     STK08, R0;
    .endif;
    .if L_STKSAVECOUNT > 9;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 9;
    BANKSEL STK09;
    MOV     STK09, R0;
    .endif;
    .if L_STKSAVECOUNT > 10;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 0x0A;
    BANKSEL STK10;
    MOV     STK10, R0;
    .endif;
    .if L_STKSAVECOUNT > 11;
    BANKSEL usebuffed_LPo;
    MOV     R0, usebuffed_LPo + 0x0B;
    BANKSEL STK11;
    MOV     STK11, R0;
    .endif;
    __endasm;
    /* PRQA S 0289, 0899, 0451, 0873, 0780, 0782, 0911, 1277, 3110, 3112, 2000, 2741, 3202, 3109, 3417, 3139-- */
    /* PRQA S 2015, 2214, 2753, 3426, 1842-- */
}
#else
void H_ISR_PUSH_STK(void) {}
void H_ISR_POP_STK(void) {}
void L_ISR_PUSH_STK(void) {}
void L_ISR_POP_STK(void) {}
#endif
/* EOF */
